package com.crane.oss.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.BizException;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.school.bean.request.QueryHeartRateReq;
import com.crane.oss.school.bean.request.heartrate.AddHeartRateReq;
import com.crane.oss.school.bean.request.heartrate.UpdateHeartRateReq;
import com.crane.oss.school.bean.resp.AreaResp;
import com.crane.oss.school.bean.resp.HeartRateResp;
import com.crane.oss.school.dao.HeartRateDao;
import com.crane.oss.school.model.HeartRate;
import com.crane.oss.school.service.AreaService;
import com.crane.oss.school.service.HeartRateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class HeartRateServiceImpl implements HeartRateService {

    @Resource
    protected HeartRateDao heartRateDao;

    @Resource
    AreaService areaService;

    @Override
    public HeartRateResp queryHeartRate(Long id) {
        log.info("HeartRateBOImpl queryHeartRate id={}", id);
        HeartRate heartRate = heartRateDao.selectById(id);
        if (null == heartRate) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "heartRate");
        }
        HeartRateResp heartRateResp = new HeartRateResp();
        BeanUtil.copyProperties(heartRate, heartRateResp);
        return heartRateResp;
    }

    @Override
    public IPage<HeartRateResp> queryList(QueryHeartRateReq queryRequest) {
        log.info("HeartRateBOImpl queryList queryRequest={}", queryRequest);
        IPage<HeartRateResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<HeartRate> queryHeartRateWrapper = new LambdaQueryWrapper<>();
        IPage<HeartRate> page = heartRateDao.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryHeartRateWrapper);
        return page.convert(u -> {
            HeartRateResp v = new HeartRateResp();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddHeartRateReq addRequest) {
        log.info("HeartRateBOImpl save request={}", addRequest);
        HeartRate heartRate = new HeartRate();
        BeanUtil.copyProperties(addRequest, heartRate);
        int i = 0;
        try {
            i = heartRateDao.insert(heartRate);
        } catch (Exception e) {
            log.info("HeartRateBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateHeartRateReq updateRequest) {
        log.info("HeartRateServiceImpl update request={}", updateRequest);
        AreaResp areaResp=areaService.queryArea(updateRequest.getSchoolId());
        if(!areaResp.getPartyId().equals(updateRequest.getPartyId())){
            throw new AppException(BasicErrorCode.NO_DATA_RIGHT);
        }
        int i = 0;
        if(null == updateRequest.getId()){
            HeartRate heartRate = new HeartRate();
            BeanUtil.copyProperties(updateRequest, heartRate);
            heartRate.setCreateTime(new Date());
            i=heartRateDao.insert(heartRate);
        }else {
            HeartRate heartRate = heartRateDao.selectById(updateRequest.getId());
            if (null == heartRate) {
                throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "heartRate");
            }
            HeartRate update = new HeartRate();
            BeanUtil.copyProperties(updateRequest, update);
            i = heartRateDao.updateById(heartRate);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("HeartRateServiceImpl delete id={}", id);
        return heartRateDao.deleteById(id);
    }

}
